package com.alex.keywordtool;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

public class Keyword {

	private String keyword;

	private Set<String> words;

	private String[] attrs;

	public static Keyword parse(String str) {
		Keyword keyword = new Keyword();
		keyword.words = new HashSet<String>();
		
		String[] parts = StringUtils.split(str, "\t");
		keyword.words.addAll(Arrays.asList(StringUtils.split(parts[0], " ")));
		keyword.attrs = Arrays.copyOfRange(parts, 1, parts.length);
		keyword.keyword = parts[0];
	
		return keyword;
	}

	public double getFloatAttr(int i) {
		try {
			return Float.parseFloat(attrs[i]);
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	public String getKeyword() {
		return keyword;
	}

	public Set<String> getWords() {
		return words;
	}

	public String[] getAttrs() {
		return attrs;
	}
}